package com.qf.business.course.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.business.course.dao.CourseDescInfoDao;
import com.qf.business.course.service.CourseDescInfoService;
import com.qf.business.course.service.CourseInfoService;
import com.qf.commons.core.exception.ServiceException;
import com.qf.data.course.entity.CourseDescInfo;
import com.qf.data.course.entity.CourseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 课程详细计划表(CourseDescInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-09-23 09:25:38
 */
@Service("courseDescInfoService")
public class CourseDescInfoServiceImpl extends ServiceImpl<CourseDescInfoDao, CourseDescInfo> implements CourseDescInfoService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private CourseInfoService courseInfoService;

    @Override
    public CourseDescInfo queryBeginCourseDesc(Integer cid) {
        CourseDescInfo courseDescInfo = getBaseMapper().queryBeginCourseDesc(cid);
        if (courseDescInfo == null) {
            throw new ServiceException(500, "还没到上课时间！");
        }

        //修改课程状态
        CourseInfo courseInfo = courseInfoService.getById(cid);
        courseInfo.setStatus(1);
        courseInfoService.updateById(courseInfo);


        //将当前课程信息添加到Redis中
        //保存当前的课程章节id
        redisTemplate.opsForHash().put("course_" + cid, "desc_id", courseDescInfo.getId() + "");
        redisTemplate.opsForHash().put("course_" + cid, "desc_title", courseDescInfo.getTitle());
        redisTemplate.opsForHash().put("course_" + cid, "online_count", "0");

        //添加到Redis
        return courseDescInfo;
    }
}

